#include "stdafx.h"
#include "RenderWindowManager.h"
#include "Mainform.h"

using namespace particleEditorCli;
RenderWindowManager::RenderWindowManager():
											mNbRw(0),
											mIsRendering(false)
{
	mInstance = this;
	mRenderWinList = gcnew System::Collections::Generic::List<RenderWindowForm^>();
	Application::Idle += gcnew EventHandler(this, &RenderWindowManager::render);
}

RenderWindowManager::~RenderWindowManager()
{
	Application::Idle -= gcnew EventHandler(this, &RenderWindowManager::render);
}

RenderWindowForm^ RenderWindowManager::addRenderWindow()
{
	RenderWindowForm^ render = gcnew RenderWindowForm(mNbRw++);
	render->Show(Mainform::Instance->Dockpanel);
	mRenderWinList->Add(render);
	return render;
}

void RenderWindowManager::removeRenderWindow(RenderWindowForm^rw)
{
	mRenderWinList->Remove(rw);
}

void RenderWindowManager::removeAllRenderWindow()
{
	for each(RenderWindowForm^ rw in mRenderWinList)
	{
		delete rw;
	}
	mRenderWinList->Clear();
}

void RenderWindowManager::startRendering()
{
	mIsRendering = true;
	while(mIsRendering)
	{
		Ogre::Root::getSingletonPtr()->renderOneFrame();
		Application::DoEvents();
	}
	//System::Threading::Thread^ t = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &RenderWindowManager::render));
	
}

void RenderWindowManager::stopRendering()
{
	mIsRendering = false;
}

void RenderWindowManager::render(Object^ obj, EventArgs^e)
{
	Ogre::Root::getSingletonPtr()->renderOneFrame();
}

RenderWindowManager^ RenderWindowManager::Instance::get()
{
	return mInstance;
}

void RenderWindowManager::removeDockpanel()
{
	for each(RenderWindowForm^ rw in mRenderWinList)
	{
		rw->DockPanel = nullptr;
	}
}

void RenderWindowManager::setDockpanel(DockPanel^ panel)
{
	for each(RenderWindowForm^ rw in mRenderWinList)
	{
		rw->DockPanel = panel;
	}
}




